package com.smallbug.longman.adapter;

import android.content.SharedPreferences;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.smallbug.longman.R;
import com.smallbug.longman.ShowWord;
import com.smallbug.longman.bean.Subsense;

import java.util.ArrayList;

import static android.content.Context.MODE_PRIVATE;


public class SubsenseAdapter extends RecyclerView.Adapter<SubsenseAdapter.SenseViewHodler>{


    private final SharedPreferences settings;
    private ShowWord context;
    private ArrayList<Subsense> data;
    private int upOrDown=0;

    public SubsenseAdapter(ShowWord context,ArrayList<Subsense> data,int upOrDown){
        this.upOrDown=upOrDown;
        this.data=data;
        this.context=context;
        settings = context.getSharedPreferences("settings", MODE_PRIVATE);
    }

    @Override
    public SenseViewHodler onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=context.getLayoutInflater().inflate(R.layout.item_sense,parent,false);
        return new SenseViewHodler(view);
    }

    @Override
    public void onBindViewHolder(SenseViewHodler holder, int position) {

        holder.sensenum.setText(position+1+"、");
        holder.def.setText(data.get(position).getDEF());
        holder.exampleRecycler.setLayoutManager(new LinearLayoutManager(context,LinearLayout.VERTICAL,false){
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        ExampleAdapter adapter= new ExampleAdapter(context,data.get(position).getExamples(),upOrDown);
        holder.exampleRecycler.setAdapter(adapter);

        switch (upOrDown){
            case 0:
                break;
            case 1:
                holder.def.setTextSize(TypedValue.COMPLEX_UNIT_PX,  holder.def.getTextSize() + 5);
                settings.edit().putFloat("text_size", holder.def.getTextSize()).apply();
                break;
            case 2:
                holder.def.setTextSize(TypedValue.COMPLEX_UNIT_PX,  holder.def.getTextSize() - 5);
                settings.edit().putFloat("text_size", holder.def.getTextSize()).apply();
                break;
            default:
                break;
        }

    }

    @Override
    public int getItemCount() {
        return data==null?0:data.size();
    }


    public ArrayList<Subsense> getData() {
        return data;
    }


    public class SenseViewHodler extends RecyclerView.ViewHolder{

        private final TextView sensenum,def;
        private final RecyclerView exampleRecycler,subsenseRecycler;

        public SenseViewHodler(View itemView) {
            super(itemView);
            sensenum=(TextView)itemView.findViewById(R.id.sensenum);
            def=(TextView)itemView.findViewById(R.id.def);
            exampleRecycler=(RecyclerView)itemView.findViewById(R.id.exampleRecycler);
            subsenseRecycler=(RecyclerView) itemView.findViewById(R.id.subsenseRecycler);

        }
    }

}
